package com.js.statemachine.controller;

import com.js.statemachine.common.BizUniformResult;
import com.js.statemachine.common.ConditionConst;
import com.js.statemachine.common.EventConst;
import com.js.statemachine.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：js
 * @date ：Created in 2021-03-02 17:10
 * @description: 测试接口
 * @version: 1.0
 */

@RestController
public class TestController {

    @Autowired
    @Qualifier("meterStateMachineFactory")
    StateMachineFactory<String, String> meterStateMachineFactory;

    @Autowired
    @Qualifier("mpStateMachineFactory")
    StateMachineFactory<String, String> mpStateMachineFactory;

    @Autowired
    @Qualifier("meterRedisUmlPersister")
    private StateMachinePersister<String, String, String> meterPersistUmlConfig;

    @Autowired
    @Qualifier("mpRedisUmlPersister")
    private StateMachinePersister<String, String, String> mpPersistUmlConfig;

    @RequestMapping("/test")
    public void test(String id) throws Exception {

        User user = new User();
        user.setId(id);
        user.setName("ASA");
        BizUniformResult<Object> result1 = new BizUniformResult<>();
        result1.setData(user);

        StateMachine<String, String> stateMachine = meterStateMachineFactory.getStateMachine("meter");
        meterPersistUmlConfig.restore(stateMachine, user.getId());
        System.out.println("meter恢复状态机后的状态为：" + stateMachine.getState().getId());

        //安装
        Message<String> message = MessageBuilder.withPayload(EventConst.INSTALL).setHeader(ConditionConst.DTO, result1).setHeader("userObj", "userObjValue")
                .setHeader("mpId", "as").build();
        stateMachine.sendEvent(message);
        //返回结果
        BizUniformResult result = (BizUniformResult) message.getHeaders().get(ConditionConst.DTO);

        System.out.println(result);

        boolean b = stateMachine.sendEvent(EventConst.MAINTAIN_COMPLETE);
        System.out.println(b + "维护完成事件成功与否");

        Message<String> message1 = MessageBuilder.withPayload(EventConst.SPLIT_METER).setHeader(ConditionConst.DTO, result1).build();
        //拆表
        stateMachine.sendEvent(message1);
        //检定
        stateMachine.sendEvent(MessageBuilder.withPayload(EventConst.
                DOCIMASY).setHeader(ConditionConst.PREMISE, ConditionConst.UNINSTALLIERT).build());
        //维护
        stateMachine.sendEvent(EventConst.MAINTAIN);

        //redis持久化
        meterPersistUmlConfig.persist(stateMachine, user.getId());

        /**
         * 计量点
         */
        StateMachine<String, String> mpMachine = mpStateMachineFactory.getStateMachine("mp1");
        mpPersistUmlConfig.restore(mpMachine, "mp1");
        System.out.println("mp恢复状态机后的状态为：" + mpMachine.getState().getId());
        //安装
        Message<String> m1 = MessageBuilder.withPayload(EventConst.OPEN).setHeader("user", user).setHeader("userObj", "userObjValue")
                .setHeader("mpId", "as").build();
        mpMachine.sendEvent(m1);
        mpMachine.setStateMachineError(new Exception());
        //redis持久化
        mpPersistUmlConfig.persist(mpMachine, "mp1");
    }

}
